package resources.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.ArrayList;

import resources.pojo.pe_atnde;
import resources.pojo.pe_deptmt;
import resources.pojo.pe_eplye;

public class AttendanceDAO extends DatabaseDAO {
	public static ArrayList<pe_atnde> getAttendance(String action, int month,
			int year, String idEmp, int idDepart) {
		String sql = "call sp_pe_attendance_select(?,?,?,?,?)";
		ArrayList<pe_atnde> temp = new ArrayList<pe_atnde>();
		try {
			cs = conn.prepareCall(sql);
			cs.setString(1, action);
			cs.setInt(2, month);
			cs.setInt(3, year);
			cs.setString(4, "%%");
			cs.setInt(5, idDepart);
			rs = cs.executeQuery();
			pe_atnde item;
			pe_eplye employee;
			while (rs.next()) {
				item = new pe_atnde();
				employee = new pe_eplye();

				employee.setId(rs.getString("a.eplyeid"));
				employee.setLtne(rs.getString("e.ltne"));
				employee.setFtne(rs.getString("e.ftne"));

				item.setCdde(rs.getTimestamp("a.cdde"));
				item.setMh(rs.getInt("a.mh"));
				item.setYr(rs.getInt("a.yr"));
				item.setDscptn(rs.getString("a.dscptn"));
				item.setDa(rs.getString("a.da"));
				item.setPe_eplye(employee);

				temp.add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return temp;
	}

	public static boolean saveDatabase(pe_atnde attendance, Connection conn,
			CallableStatement cs) {
		String sql = "call sp_pe_attendance(?,?,?,?,?,?)";
		try {
			cs = conn.prepareCall(sql);
			cs.setString(1, "create");
			cs.setInt(2, attendance.getMh());
			cs.setInt(3, attendance.getYr());
			cs.setString(4, attendance.getPe_eplye().getId());
			cs.setString(5, attendance.getDscptn());
			cs.setString(6, attendance.getDa());
			cs.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public static boolean save(ArrayList<pe_atnde> list) {
		try {
			for (pe_atnde item : list) {
				saveDatabase(item, conn, cs);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
